﻿Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports QBuilder


<TestClass()> _
Public Class CriteriaTest


    Private testContextInstance As TestContext

    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(value As TestContext)
            testContextInstance = value
        End Set
    End Property

#Region "String Tests"
    <TestMethod()>
    Public Sub Criteria_String_Contains()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.Contains, "Cat")
        Dim expected As String = "A LIKE '%Cat%'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_EndsWith()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.EndsWith, "Cat")
        Dim expected As String = "A LIKE '%Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_EqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.EqualTo, "Cat")
        Dim expected As String = "A = 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_GreaterThan()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.GreaterThen, "Cat")
        Dim expected As String = "A > 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_LessThan()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.LessThen, "Cat")
        Dim expected As String = "A < 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_StartsWith()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.StartsWith, "Cat")
        Dim expected As String = "A LIKE 'Cat%'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_NotEqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.NotEqualTo, "Cat")
        Dim expected As String = "A <> 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_Like()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.Like, "C?t")
        Dim expected As String = "A LIKE 'C?t'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_Contains_Escaped()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.Contains, "%Cat")
        Dim expected As String = "A LIKE '%[%]Cat%'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_NotEscaped()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.EqualTo, "C%t")
        Dim expected As String = "A = 'C%t'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_Contains_Numeric()
        Dim target As Criteria = New Criteria("A", Operation.Contains, "12")
        Dim expected As String = "A LIKE '%12%'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_StartWith_Numeric()
        Dim target As Criteria = New Criteria("A", Operation.StartsWith, "12")
        Dim expected As String = "A LIKE '12%'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_String_EndWith_Numeric()
        Dim target As Criteria = New Criteria("A", Operation.EndsWith, "12")
        Dim expected As String = "A LIKE '%12'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region

#Region "Numeric Tests"
    '<TestMethod()>
    '<ExpectedException(GetType(InvalidOperationException))>
    'Public Sub Criteria_Numeric_Contains()
    '    Dim target As ICriteriaComponent = New Criteria("A", Operation.Contains, "123") With {.RightExpressionType = ExpressionType.Literal}
    'End Sub

    '<TestMethod()>
    '<ExpectedException(GetType(InvalidOperationException))>
    'Public Sub Criteria_Numeric_EndsWith()
    '    Dim target As ICriteriaComponent = New Criteria("A", Operation.EndsWith, "123")
    'End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_EqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.EqualTo, "123") With {.RightExpressionType = ExpressionType.Literal}
        Dim expected As String = "A = 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_GreaterThan()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.GreaterThen, "123")
        Dim expected As String = "A > 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_LessThan()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.LessThen, "123")
        Dim expected As String = "A < 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    '<TestMethod()>
    '<ExpectedException(GetType(InvalidOperationException))>
    'Public Sub Criteria_Numeric_StartsWith()
    '    Dim target As ICriteriaComponent = New Criteria("A", Operation.StartsWith, "123")
    'End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_NotEqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.NotEqualTo, "123")
        Dim expected As String = "A <> 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    '<TestMethod()>
    '<ExpectedException(GetType(InvalidOperationException))>
    'Public Sub Criteria_Numeric_Like()
    '    Dim target As ICriteriaComponent = New Criteria("A", Operation.Like, "123")
    'End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_GreaterThanEqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.GreaterThanEqualTo, "123")
        Dim expected As String = "A >= 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_Numeric_LessThanEqualTo()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.LessThanEqualTo, "123")
        Dim expected As String = "A <= 123"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region

#Region "Parameter Tests"
    <TestMethod()>
    Public Sub Criteria_String_EqualTo_Param()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.EqualTo, "@Param1")
        Dim expected As String = "A = @Param1"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region

#Region "NULL Tests"

    <TestMethod()> _
    Public Sub Criteria_IsNull1()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.IsNULL, "Cat")
        Dim expected As String = "A IS NULL"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Verifies that IsNULL criteria ignores the pattern
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()> _
    Public Sub Criteria_IsNull2()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.IsNULL, "NULL")
        Dim expected As String = "A IS NULL"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()> _
    Public Sub Criteria_IsNotNull1()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.IsNotNULL, "NULL")
        Dim expected As String = "A IS NOT NULL"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    ''' <summary>
    ''' Verifies that IsNotNULL criteria ignores the pattern
    ''' </summary>
    ''' <remarks></remarks>
    <TestMethod()> _
    Public Sub Criteria_IsNotNull2()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.IsNotNULL, "Cat")
        Dim expected As String = "A IS NOT NULL"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region

#Region "Negation Tests"
    <TestMethod()>
    Public Sub Criteria_Negation_Param()
        Dim target As Criteria = New Criteria("A", Operation.EqualTo, "@Param1")
        target.Negate = True
        Dim expected As String = "NOT A = @Param1"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region


#Region "Operand Type"
    <TestMethod()>
    Public Sub Criteria_OperandType_Auto()
        Dim target As Criteria = New Criteria("A", Operation.GreaterThen, "Cat") With {.RightExpressionType = ExpressionType.Auto}
        Dim expected As String = "A > 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)

        target.RightExpression = "123"
        expected = "A > 123"
        actual = target.GetText
        Assert.AreEqual(expected, actual)

        target.RightExpression = "@Cat"
        expected = "A > @Cat"
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_OperandType_Field()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.GreaterThen, "Cat") With {.RightExpressionType = ExpressionType.Literal}
        Dim expected As String = "A > 'Cat'"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub

    <TestMethod()>
    Public Sub Criteria_OperandType_Literal()
        Dim target As ICriteriaComponent = New Criteria("A", Operation.GreaterThen, "Cat") With {.RightExpressionType = ExpressionType.Logical}
        Dim expected As String = "A > Cat"
        Dim actual As String
        actual = target.GetText
        Assert.AreEqual(expected, actual)
    End Sub
#End Region

End Class
